class Solution {
public:
    int calculate(string s) {
        int n = s.size(), i = 0;
        vector<int> stack(n);
        char op = '+';
        while(i < n)
        {
            if(s[i] == ' ')
                ++i;
            else if(s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while(i < n && s[i] >= '0' && s[i] <= '9')
                {
                    tmp = tmp * 10 + (s[i++] - '0'); // 提取数字
                }
                if(op == '+')
                        stack.push_back(tmp);
                else if(op == '-')
                        stack.push_back(-tmp);
                else if(op == '*')
                        stack.back() *= tmp;
                else // (op == '/')
                        stack.back() /= tmp;
            }
            else
                op = s[i++];
        }
        int sum = 0;
        for(auto& e : stack)
        {
            sum += e;
        }
        return sum;
    }
};